import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class testJDBC {
    public static void main(String[] args) throws  SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("284703");
        // 2. 建立连接
        Connection connection = dataSource.getConnection();
        // 3. 构造 SQL
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        // 4. 执行 SQL
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            // 这个代码, 就是获取 "id" 这一列的数值. 期望得到一个 int
            int id = resultSet.getInt("id");
            // 这个代码, 就是获取 "name" 这一列的数值. 期望得到一个 String
            String name = resultSet.getString("name");
            System.out.print("id: " + id+" ");
            System.out.println("name: " + name+" ");
        }

        // 5. 释放资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
